﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XCI.Core;

namespace XCI.Windows.Extensions
{
    public static class BoolMessageExtensions
    {
        /// <summary>
        /// 成功回调函数
        /// </summary>
        public static BoolMessage IfSuccess(this BoolMessage boolMessage, Action<BoolMessage> action)
        {
            action?.Invoke(boolMessage);
            return boolMessage;
        }

        /// <summary>
        /// 失败回调函数
        /// </summary>
        public static BoolMessage IfFail(this BoolMessage boolMessage, Action<BoolMessage> action)
        {
            action?.Invoke(boolMessage);
            return boolMessage;
        }

        /// <summary>
        /// 确保Api消息成功,否则抛出异常
        /// </summary>
        public static BoolMessage EnsureSuccess(this BoolMessage boolMessage)
        {
            if (!boolMessage.Success)
            {
                throw new WebApiException(boolMessage.Msg);
            }
            return boolMessage;
        }

        /// <summary>
        /// 确保验证消息成功,否则抛出异常
        /// </summary>
        public static BoolMessage EnsureValidSuccess(this BoolMessage boolMessage)
        {
            if (!boolMessage.Success)
            {
                throw new BoolMessageValidException(boolMessage.Msg);
            }
            return boolMessage;
        }
    }

    public static class BoolMessageTExtensions
    {
        /// <summary>
        /// 成功回调函数
        /// </summary>
        public static BoolMessage<T> IfSuccess<T>(this BoolMessage<T> boolMessage, Action<BoolMessage<T>> action)
        {
            action?.Invoke(boolMessage);
            return boolMessage;
        }

        /// <summary>
        /// 失败回调函数
        /// </summary>
        public static BoolMessage<T> IfFail<T>(this BoolMessage<T> boolMessage, Action<BoolMessage<T>> action)
        {
            action?.Invoke(boolMessage);
            return boolMessage;
        }

        /// <summary>
        /// 确保Api消息成功,否则抛出异常
        /// </summary>
        public static BoolMessage<T> EnsureSuccess<T>(this BoolMessage<T> boolMessage)
        {
            if (!boolMessage.Success)
            {
                throw new WebApiException(boolMessage.Msg);
            }
            return boolMessage;
        }

        /// <summary>
        /// 转为消息对象
        /// </summary>
        public static BoolMessage AsMessage<T>(this BoolMessage<T> boolMessage)
        {
            return new BoolMessage(boolMessage.Success, boolMessage.Msg, boolMessage.Data);
        }
    }
}
